package com.holyn.guofang.view.web;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.ServiceTypeWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.web.WebContract;
import com.holyn.guofang.presenter.web.WebPresenter;
import com.holyn.guofang.utils.ValidUtil;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.popupwindow.PopupSingleSelectFromBottom;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/13.
 */

public class WebActivity extends BaseActivity implements BaseView, WebContract.WebView{
    private static final int REQUEST_ACTION_SERVICE_TYPE = 0x0001;
    private static final int REQUEST_ACTION_ADD_MARK = 0x0002;

    @BindView(R.id.tv_select_code)
    TextView tvSelectCode;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.et_contact)
    EditText etContact;
    @BindView(R.id.et_QQ)
    EditText etQQ;
    @BindView(R.id.et_telephone)
    EditText etTelephone;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    /**
     * 获取网上服务事宜分类列表。
     */
    String code = "";
    Integer parentID;
    Integer grade = 2;

    /**
     * 提交商标事宜在线委托。
     */
    Integer id = 1;
    Integer serviceID;
    String content;
    String company;
    String contact;
    String QQ;
    String telephone;
    String email;

    private  WebContract.Presenter presenter;

    private List<ServiceTypeWo.DataBean> dataBeanList = new ArrayList<>();

    private PopupSingleSelectFromBottom popupSelectCode = null;
    private int curSelectCodePosition = 0;


    public static void start(Activity activity) {
        activity.startActivity(new Intent(activity, WebActivity.class));
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_web;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("商标事宜在线委托");
        presenter = new WebPresenter(this);
        presenter.addWebViewListener(this);
        presenter.postListServiceType(REQUEST_ACTION_SERVICE_TYPE, code, parentID, grade);
    }

    @OnClick({R.id.tv_select_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_select_code:
                if (popupSelectCode != null){
                    popupSelectCode.showPopupWindow();
                }
                break;
            case R.id.btn_submit:
                submitData();
                break;
        }
    }

    private void submitData(){
        if (dataBeanList.size() == 0){
            toastMsg("没选择委托事宜");
            return;
        }
        serviceID = dataBeanList.get(curSelectCodePosition).getId();

        content = etContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)){
            toastMsg("摘要不能为空");
            return;
        }

        company = etCompany.getText().toString().trim();
        if (TextUtils.isEmpty(company)){
            toastMsg("公司名称不能为空");
            return;
        }

        contact = etContact.getText().toString().trim();
        if (TextUtils.isEmpty(contact)){
            toastMsg("联系人不能为空");
            return;
        }

        QQ = etQQ.getText().toString().trim();

        telephone = etTelephone.getText().toString().trim();
        if (TextUtils.isEmpty(telephone)){
            toastMsg("电话不能为空");
            return;
        }
        if (!ValidUtil.isTelephone(telephone)) {
            showToastDialog("请输入正确的电话号码");
            return;
        }

        email = etEmail.getText().toString().trim();

        presenter.postAddTradeMarkTrust(REQUEST_ACTION_ADD_MARK, id, serviceID, content, company, contact, QQ, telephone, email);

    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(int requestAction, ServiceTypeWo serviceTypeWo) {
        if (serviceTypeWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            dataBeanList.clear();
            dataBeanList = serviceTypeWo.getData();

            Iterator<ServiceTypeWo.DataBean> iterator = dataBeanList.iterator();
            while (iterator.hasNext()){
                ServiceTypeWo.DataBean dataBean = iterator.next();
                if (dataBean.getName().equals("其他")){
                    iterator.remove();
                }
            }

            ServiceTypeWo.DataBean dataBean = new ServiceTypeWo.DataBean();
            dataBean.setId(0);
            dataBean.setGrade(0);
            dataBean.setCode("0000");
            dataBean.setName("其他");
            dataBeanList.add(dataBean);

            tvSelectCode.setText(dataBeanList.get(0).getName());

            initPopupSelectCode();
        } else {
            toastMsg("获取事宜数据失败");
        }
    }

    @Override
    public void responseSuccess(int requestAction, StatusWo statusWo) {
        if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            toastMsg("提交成功！");
            reSetData(true);
        } else {
            toastMsg("提交失败！");
        }
    }

    private void initPopupSelectCode(){
        List<String> itemList = new ArrayList<>();
        for (ServiceTypeWo.DataBean dataBean: dataBeanList){
            if (dataBean.getId() == 0){
                itemList.add("其他");
            } else {
                itemList.add(dataBean.getName());
            }
        }
        popupSelectCode = new PopupSingleSelectFromBottom(WebActivity.this, "请选择编号", itemList);
        popupSelectCode.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
            @Override
            public void onItemClick(String item, int position) {
                curSelectCodePosition = position;
                tvSelectCode.setText(item);

                reSetData(false);
            }
        });
    }

    private void reSetData(boolean isReSetSelectCode){
        if (isReSetSelectCode){
            curSelectCodePosition = 0;
            tvSelectCode.setText("");
        }
        etContent.setText("");
        etCompany.setText("");
        etContact.setText("");
        etQQ.setText("");
        etTelephone.setText("");
        etEmail.setText("");
    }
}
